//
//  ImageCache.m
//  极致荟
//
//  Created by hetao on 14-6-9.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "ImageCache.h"

@implementation ImageCache

///返回图片缓存文件夹路径
- (NSString *)filepath
{
    //获取沙盒Documents文件夹路径
    NSString *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *imagepath= [path stringByAppendingString:@"/image"];
    NSFileManager *file=[NSFileManager defaultManager];
    NSError *error=nil;
    
    //创建文件夹
    BOOL ok=[file createDirectoryAtPath:imagepath withIntermediateDirectories:YES attributes:nil error:&error];
    if(ok)
    {
        //NSLog(@"创建文件夹成功");
    }else
    {
        NSLog(@"创建文件夹失败");
    }
    return imagepath;
}

+(void)imageUrl:(NSString *)url UiImageview:(UIImageView *)imageview
{
    [imageview setImage:[UIImage imageNamed:@"类别页.jpg"]];
    NSString *imagepath;
    NSArray *strarr=[[NSArray alloc]init];
    strarr=[url componentsSeparatedByString:@"/"];
    NSString *name;
    if([strarr count]!=0)
    {
        name=[strarr objectAtIndex:strarr.count-1];
    }
   static ImageCache *imagecache;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        imagecache=[[ImageCache alloc]init];
    });
    if(imagecache)
    {
        imagepath=[imagecache filepath];;
    }else
    {
        NSLog(@"对象不存在");
    }
    
    if(imagecache)
    {
        NSData *data=[NSData dataWithContentsOfFile:[imagepath stringByAppendingFormat:@"/%@",name]];
        UIImage *img=[UIImage imageWithData:data];
       
        if (img)
        {
            [imageview setImage:img];
        }else
        {
           
           dispatch_async(dispatch_get_global_queue(0, 0), ^{
               NSFileManager *file=[NSFileManager defaultManager];
               NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
               if(data)
               {
               [file createFileAtPath:[imagepath stringByAppendingFormat:@"/%@",name] contents: data attributes:nil];
               }else
               {
                   return ;
               }
               
               dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                   
                   [imageview setImage:[UIImage imageWithContentsOfFile:[imagepath stringByAppendingFormat:@"/%@",name]]];
               });
              
           });
            
                
                
            
//            return [UIImage imageWithData:[NSData dataWithContentsOfFile:[imagepath stringByAppendingFormat:@"/%@",name]]];
        }
    }
    
    //return nil;
}
+ (void)saveImage:(NSString *)fileName imageData:(NSData *)data
{
     NSString *imagepath;
    static ImageCache *imagecache;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        imagecache=[[ImageCache alloc]init];
    });
    if(imagecache)
    {
        imagepath=[imagecache filepath];
    }else
    {
        NSLog(@"对象不存在");
        
    }
    NSFileManager *file=[NSFileManager defaultManager];
    [file createFileAtPath:[imagepath stringByAppendingFormat:@"/%@",fileName] contents: data attributes:nil];
       
}
+ (void)dorpImage:(NSString *)filePath
{
    NSString *imagepath;
    NSArray *strarr=[[NSArray alloc]init];
    strarr=[filePath componentsSeparatedByString:@"/"];
    NSString *name;
    if([strarr count]!=0)
    {
        name=[strarr objectAtIndex:strarr.count-1];
    }
    static ImageCache *imagecache;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        imagecache=[[ImageCache alloc]init];
    });
    if(imagecache)
    {
        imagepath=[imagecache filepath];;
    }else
    {
        NSLog(@"对象不存在");
    }

    NSFileManager *file=[NSFileManager defaultManager];
    if([file fileExistsAtPath:[imagepath stringByAppendingFormat:@"/%@",name]])
    {
        [file removeItemAtPath:[imagepath stringByAppendingFormat:@"/%@",name] error:nil];
    }
}
@end
